package com.bank.bank.transaction.controller;


import com.bank.bank.common.base.pojo.dto.DrawMoneyParam;
import com.bank.bank.common.base.pojo.dto.SaveMoneyParam;
import com.bank.bank.common.base.pojo.dto.TransferMoneyParam;
import com.bank.bank.common.base.pojo.entity.Transaction;
import com.bank.bank.common.base.pojo.vo.DrawMoneyVO;
import com.bank.bank.common.base.pojo.vo.SaveMoneyVO;
import com.bank.bank.common.base.pojo.vo.TransactionDetailVO;
import com.bank.bank.common.base.response.JsonResult;
import com.bank.bank.transaction.service.TransactionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/transactions")
public class TransactionController {
    @Autowired
    private TransactionService transactionService;

    /**
     * 1.存款功能
     * @param saveMoneyParam to_account_id、amount
     * @return
     */
    @PostMapping("/deposit")
    public JsonResult deposit(@RequestBody SaveMoneyParam saveMoneyParam){
        log.debug("saveMoneyParam:{}", saveMoneyParam);
        SaveMoneyVO saveMoneyVO = transactionService.deposit(saveMoneyParam);
        return JsonResult.ok(saveMoneyVO);
    }

    /**
     * 2.取款功能
     */
    @PostMapping("/withdraw")
    public JsonResult withdraw(@RequestBody DrawMoneyParam drawMoneyParam){
        log.debug("drawMoneyParam:{}", drawMoneyParam);
        DrawMoneyVO drawMoneyVO = transactionService.withdraw(drawMoneyParam);
        return JsonResult.ok(drawMoneyVO);
    }

    /**
     * 3.转账功能
     */
    @PostMapping("/transfer")
    public JsonResult transfer(@RequestBody TransferMoneyParam transferMoneyParam){
        log.debug("transferMoneyParam:{}", transferMoneyParam);
        transactionService.transfer(transferMoneyParam);
        return JsonResult.ok();
    }

    /**
     * 4.查看指定交易记录的详情
     */
    @GetMapping("/{transactionId}")
    public JsonResult getTransaction(@PathVariable("transactionId") String transactionId){
        log.debug("transactionId:{}", transactionId);
        TransactionDetailVO transactionDetailVO = transactionService.getTransaction(transactionId);
        return JsonResult.ok(transactionDetailVO);
    }

    /**
     * 查询某个账号的交易记录
     */
    @GetMapping("/list/{accountId}")
    public JsonResult getAccountTrans(@PathVariable Integer accountId){
        List<Transaction> trans= transactionService.getAccountTrans(accountId);
        return JsonResult.ok(trans);
    }
}
















